package com.rfchina.okr.xianren;

import java.sql.Connection;
import java.util.Base64;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import com.rfchina.okr.xianren.base.ErrorActionInterceptor;
import com.rfchina.okr.xianren.controller.admin.XianrenAdminRoutes;
import com.rfchina.okr.xianren.controller.api.XianrenApiRoutes;
import com.rfchina.okr.xianren.manager.ExecutorServiceManager;
import com.rfchina.okr.xianren.model._MappingKit;
import com.rfchina.okr.xianren.task.FinishTaskInfo;
import com.rfchina.okr.xianren.task.TaskAudit;
import com.rfchina.okr.xianren.task.TaskRechargeInfo;
import com.rfchina.okr.xianren.util.DateTimeUtil;

public class XianRenServerJfinalConfig extends JFinalConfig {
	private static Logger logger = LoggerFactory.getLogger(XianRenServerJfinalConfig.class);

	@Override
	public void afterJFinalStart() {

		logger.error("-------my is error---" + DateTimeUtil.formatDateTime(new Date()));

		ExecutorServiceManager.getInstance().scheduleWithFixedDelay(new TaskAudit(), 2, 10);
		ExecutorServiceManager.getInstance().scheduleWithFixedDelay(new TaskRechargeInfo(), 1, 10);
		ExecutorServiceManager.getInstance().scheduleWithFixedDelay(new FinishTaskInfo(), 5, 60);

	}

	private static Prop p = loadConfig();

	@Override
	public void configConstant(Constants arg0) {
		arg0.setDevMode(p.getBoolean("devMode", false));
		arg0.setViewType(ViewType.FREE_MARKER);
		// arg0.setJsonFactory(new JacksonFactory());
		// arg0.setError404View("404.html");
	}

	private static Prop loadConfig() {
		String config = System.getProperty("jfinal_config", "dev");
		logger.info("config:{}", config);
		try {
			return PropKit.use(String.format("config_%s.properties", config));
		} catch (Exception e) {
			logger.info(e.getMessage());
			return PropKit.use("config_dev.properties");
		}
	}

	@Override
	public void configEngine(Engine arg0) {

	}

	@Override
	public void configHandler(Handlers arg0) {
		// arg0.add(new DoAndJsonHandlers());
	}

	@Override
	public void configInterceptor(Interceptors arg0) {
		arg0.addGlobalActionInterceptor(new ErrorActionInterceptor());

	}

	private WallFilter wallFilter;

	@Override
	public void configPlugin(Plugins me) {
		DruidPlugin druidPlugin = getDruidPlugin(p);
		wallFilter = new WallFilter();
		wallFilter.setDbType("mysql");
		druidPlugin.addFilter(wallFilter);
		druidPlugin.addFilter(new StatFilter());
		me.add(druidPlugin);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		arp.setTransactionLevel(Connection.TRANSACTION_READ_COMMITTED);
		_MappingKit.mapping(arp);

		// arp.setPrimaryKey("line_place_rela", "line_id,place_id");
		me.add(arp);
		if (p.getBoolean("devMode", false)) {
			arp.setShowSql(true);
		}
		// me.add(new Cron4jPlugin(PropKit.use("task.properties")));

		String name = p.get("redis.name", "root");
		String host = p.get("redis.host");
		int port = p.getInt("redis.port", 6379);
		// 配置缓存插件
		RedisPlugin redis = new RedisPlugin(name, host, port);
		me.add(redis);
	}

	public static DruidPlugin getDruidPlugin(Prop p) {
		return new DruidPlugin(p.get("jdbcUrl"), p.get("user"),
				new String(Base64.getDecoder().decode(p.get("password").trim())));
	}

	@Override
	public void configRoute(Routes arg0) {

		arg0.add(new XianrenAdminRoutes());
		arg0.add(new XianrenApiRoutes());

	}

	public static void main(String[] args) {
		JFinal.start("src/main/webapp", 10601, "/", 5);

	}

}
